package com.newjiabo.applet.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.newjiabo.common.annotation.Log;
import com.newjiabo.common.core.controller.BaseController;
import com.newjiabo.common.core.domain.AjaxResult;
import com.newjiabo.common.core.enums.ResultCode;
import com.newjiabo.common.core.page.TableDataInfo;
import com.newjiabo.common.enums.BusinessType;
import com.newjiabo.common.exception.ServiceException;
import com.newjiabo.common.utils.PageUtils;
import com.newjiabo.common.utils.StringUtils;
import com.newjiabo.ski.domain.SkiAfterSale;
import com.newjiabo.ski.domain.SkiOrder;
import com.newjiabo.ski.domain.dto.AssessDto;
import com.newjiabo.ski.domain.vo.AfterSaleOrderQuery;
import com.newjiabo.ski.domain.vo.OrderQuery;
import com.newjiabo.ski.service.ISkiAfterSaleService;
import com.newjiabo.ski.service.ISkiOrderService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * {个人订单中心}
 *
 * @author CaoMengHui.
 * @version 1.0
 * @since 2023/9/25 13:54
 */
@RestController
@RequestMapping(value = "/applet/order")
@Api(tags = "小程序;订单中心")
public class OrderAppletController extends BaseController {

    @Resource
    private ISkiOrderService orderService;
    @Resource
    private ISkiAfterSaleService afterSaleService;

    @ApiOperation(value = "获取订单列表信息")
    @GetMapping("/getOrderList")
    public TableDataInfo getOrderList(OrderQuery orderQuery) {
        startPage();
        SkiOrder skiOrder = new SkiOrder();
        skiOrder.setOrderStatus(orderQuery.getOrderStatus());
        String memberId = getLoginUser().getMemberId();
        if (StringUtils.isEmpty(memberId)) {
            throw new ServiceException(ResultCode.USER_AUTH_EXPIRED);
        }
        skiOrder.setMemberId(memberId);
        List<SkiOrder> list = orderService.selectSkiOrderList(skiOrder);
        return getDataTable(list);
    }

    @ApiOperation(value = "查看订单详情")
    @GetMapping(value = "/getOrderInfo/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable String orderId) {
        return AjaxResult.success(orderService.getInfoDetail(orderId));
    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(orderService.deleteSkiOrderByOrderIds(orderIds));
    }

    @ApiOperation(value = "获取售后订单列表信息")
    @GetMapping("/afterSale/getList")
    public TableDataInfo afterSalesList(AfterSaleOrderQuery query) {
        startPage();
        SkiAfterSale skiAfterSale = new SkiAfterSale();
        skiAfterSale.setServiceStatus(query.getAfterSalesStatus());
        return getDataTable(afterSaleService.selectSkiAfterSaleList(skiAfterSale));
    }

    /**
     * 删除售后
     */
    @ApiOperation(value = "删除售后")
    @DeleteMapping("/afterSale/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(afterSaleService.deleteSkiAfterSaleByIds(ids));
    }

    @ApiOperation(value = "完成订单，确认收货")
    @GetMapping(value = "/completeOrder/{orderId}")
    public AjaxResult completeOrder(@PathVariable @NotNull String orderId){
        return toAjax(orderService.completeOrder(orderId));
    }



}
